/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gouwudai.table.view.style2;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.DefaultStatusBar;
import org.extremecomponents.table.view.View;
import org.extremecomponents.table.view.html.CalcBuilder;
import org.extremecomponents.table.view.html.FormBuilder;
import org.extremecomponents.table.view.html.RowBuilder;
import org.extremecomponents.table.view.html.TableBuilder;
import org.extremecomponents.util.HtmlBuilder;

import com.gouwudai.table.view.style1.UserPreferencesConstants;


/**
 * @author Eccn-sunpauliz
 */
public class ExHtmlView implements View {
    private HtmlBuilder html;
    private TableBuilder tableBuilder;
    private RowBuilder rowBuilder;
    private FormBuilder formBuilder;
    private CalcBuilder calcBuilder;
    
    //为提高效率，将两个工具栏提取出来
    private ExToolbar toolbarEx;
    private DefaultStatusBar defaultStatusBar;
    
    boolean bufferView;
    
    protected HtmlBuilder getHtmlBuilder() {
        return html;
    }

    protected CalcBuilder getCalcBuilder() {
        return calcBuilder;
    }

    protected FormBuilder getFormBuilder() {
        return formBuilder;
    }

    protected RowBuilder getRowBuilder() {
        return rowBuilder;
    }

    protected TableBuilder getTableBuilder() {
        return tableBuilder;
    }
    
    public void beforeBody(TableModel model) {
        bufferView = model.getTableHandler().getTable().isBufferView();
        if (bufferView) {
            html = new HtmlBuilder();
        } else {
            html = new HtmlBuilder(model.getContext().getWriter());
        }

        tableBuilder = new TableBuilder(html, model);
        rowBuilder = new RowBuilder(html, model);
        formBuilder = new FormBuilder(html, model);
        calcBuilder = new CalcBuilder(html, model);

        formBuilder.formStart();

        tableBuilder.themeStart();

        tableBuilder.tableStart();

        tableBuilder.theadStart();

        //tableBuilder.title();
        tableBuilder.titleRowSpanColumns();
        //tableBuilder.titleRowSpanColumns();

        String paginationLocation = (String)model.getTableHandler().getTable().getAttribute("paginationLocation");
        if(paginationLocation==null)
        	paginationLocation = model.getPreferences().getPreference(ExPreferencesConstants.TABLE_PAGINATIONLOCATION);
        if (ExTableConstants.PAGINATION_LOCATION_BOTH.equalsIgnoreCase(paginationLocation)||ExTableConstants.PAGINATION_LOCATION_UP.equalsIgnoreCase(paginationLocation)) {
        	//defaultStatusBar = new DefaultStatusBar(getHtmlBuilder(), model);
        	//defaultStatusBar.layout();
        	toolbarEx = new ExToolbar(getHtmlBuilder(), model);
        	toolbarEx.layout();        	
        	//toolbar(model);
			//statusBar(model);
			tableBuilder.filterRow();
		}

        tableBuilder.headerRow();

        tableBuilder.theadEnd();

        tableBuilder.tbodyStart();
        
    }
    
    public void body(TableModel model, Column column) {
        if (column.isFirstColumn()) {
            rowBuilder.rowStart();
        }

        html.append(column.getCellDisplay());

        if (column.isLastColumn()) {
            rowBuilder.rowEnd();
        }
    }
	
    public Object afterBody(TableModel model) {
        calcBuilder.defaultCalcLayout();

        String paginationLocation = (String)model.getTableHandler().getTable().getAttribute("paginationLocation");
        if(paginationLocation==null)
        	paginationLocation = model.getPreferences().getPreference(ExPreferencesConstants.TABLE_PAGINATIONLOCATION);
        

        tableBuilder.tbodyEnd();

        if (ExTableConstants.PAGINATION_LOCATION_DOWN.equalsIgnoreCase(paginationLocation)||ExTableConstants.PAGINATION_LOCATION_BOTH.equalsIgnoreCase(paginationLocation)) {
        	//tableBuilder.filterRow(); //下半部就不需要过滤行了，需要的话把注释去掉
        	if(defaultStatusBar==null)
        		//defaultStatusBar = new DefaultStatusBar(getHtmlBuilder(), model);
        	//defaultStatusBar.layout();
        	//statusBar(model);
			if(toolbarEx==null)
				toolbarEx = new ExToolbar(getHtmlBuilder(), model);
        	toolbarEx.layout();			
			//toolbar(model);
		}
        
        tableBuilder.tableEnd();

        tableBuilder.themeEnd();

        formBuilder.formEnd();
        
        if (this.bufferView) {
            return html.toString();
        }

        return "";
    }
    
    protected void toolbar(TableModel model) {
        new ExToolbar(getHtmlBuilder(), model).layout();
    }

    protected void statusBar(TableModel model) {
        new DefaultStatusBar(getHtmlBuilder(), model).layout();
    }
    
    protected void titleBar(TableModel model) {
        new DefaultStatusBar(getHtmlBuilder(), model).layout();
    }
}
